$(document).ready(function(){
	loginlogFrame();
	closeloginlogFrame();
	//初始化表格
	$('#dataGridTable').datagrid({
		columns:[[ 
		   {field:'workNumber',title:'工号',width:80,align:'center'},
           {field:'userName',title:'用户名',width:120,align:'center'}, 
           {field:'realName',title:'真实姓名',width:90,align:'center'},
           {field:'loginIp',title:'登录IP',width:90,align:'center'},
           {field:'loginType',title:'类型',width:60,align:'center',formatter:loginTypeFormater},
           {field:'loginResult',title:'操作',width:140,align:'center',formatter:loginResultFormater},
           {field:'loginTime',title:'登录时间',width:130,align:'center'},
           {field:'onlineTime',title:'在线时长',width:100,align:'center'},
           {field:'browserMessage',title:'浏览器类型',width:100,align:'center'},
           {field:'createTime',title:'记录建立时间',width:createTimeWidth,align:'center'}
         ]]
	});	
	
	$('#workNumber').textbox('textbox').keydown(function (e) {
		   if (e.keyCode == 13) {
			   $('#workNumber').textbox('setValue', $(this).val());
			   query();
		    }
		});
	
	$('#userName').textbox('textbox').keydown(function (e) {
	   if (e.keyCode == 13) {
		   $('#userName').textbox('setValue', $(this).val());
		   query();
	    }
	});
	$('#realName').textbox('textbox').keydown(function (e) {
		   if (e.keyCode == 13) {
			   $('#realName').textbox('setValue', $(this).val());
			   query();
		    }
		});
	$('#startDate').textbox('textbox').keydown(function (e) {
		   if (e.keyCode == 13) {
			   $('#startDate').textbox('setValue', $(this).val());
			   query();
		    }
		});
	$('#endDate').textbox('textbox').keydown(function (e) {
		   if (e.keyCode == 13) {
			   $('#endDate').textbox('setValue', $(this).val());
			   query();
		    }
		});
});

//类型
function loginTypeFormater(value,row){
	var loginResult = row.loginResult;
	var loginType='';
	if(loginResult=='1'){
		loginType="<image src='../../resources/images/icons/yes.png'/>";
	}else if(loginResult=='2'){
		loginType="<image src='../../resources/images/icons/stop.png'/>";
	}else if(loginResult=='3'){
		loginType="<image src='../../resources/images/icons/delete.png'/>";
	}else{
		loginType='';
	}
	return loginType;
};

//操作
function loginResultFormater(value,row){
	var loginResult = row.loginResult;
	if(loginResult=='1'){
		loginResult='登录成功';
	}else if(loginResult=='2'){
		loginResult='登录失败：该用户账户不存在!';
	}else if(loginResult=='3'){
		loginResult='登录失败：该用户账户密码错误!';
	}
	return loginResult;
};

//刷新
function reload(){
	$("#workNumber").textbox('setValue','');
	$("#userName").textbox('setValue','');
 	$("#realName").textbox('setValue','');
 	$("#startDate").datebox('setValue','');
 	$("#endDate").datebox('setValue','');
	query();
}

//查询
function query(){
	$("#dataGridTable").datagrid('reload',{
		'loginlog.workNumber' : $("#workNumber").val(),
	 	'loginlog.userName' : $("#userName").val(),
	 	'loginlog.realName' :$("#realName").val(),
	 	'loginlog.startDate' :$("#startDate").datebox('getValue'),
	 	'loginlog.endDate' :$("#endDate").datebox('getValue')
	}); 
}

//初始化详细信息模态框
function loginlogFrame(){
	$("#loginlogFrame").dialog({
		title:'日志详细信息',
		modal:true,
		collapsible:true,
		resizable:false
	});
}

//关闭详细信息模态框
function closeloginlogFrame(){
	$('#loginlogFrame').dialog('close');
}

//双击行打开详细信息表
function loginlogFrameMod(){
	if($('#dataGridTable').datagrid('getSelected')){
		var logId = $('#dataGridTable').datagrid('getSelections')[0].logId;
		$.ajax({
			async : false,
			cache:false,
			type: 'POST',
			dataType : "json",
			url: 'sys/queryLoginlog.action',//请求的action路径
			data : {
				"loginlog.logId":logId
			},
			success:function(data,action,opt){
				$("#details_workNumber").text(data.rows[0].workNumber==null ? "" : data.rows[0].workNumber);
				$("#details_userName").text(data.rows[0].userName==null ? "" : data.rows[0].userName);
				$("#details_realName").text(data.rows[0].realName==null ? "" : data.rows[0].realName);
				$("#details_loginIp").text(data.rows[0].loginIp==null ? "" : data.rows[0].loginIp);
				if(data.rows[0].loginResult=='1'){
					$("#details_loginResult").text("登录成功");
					$("#details_loginType").attr("src","../../resources/images/icons/yes.png");
				} else if(data.rows[0].loginResult=='2'){
					$("#details_loginResult").text("登录失败：该用户账户不存在!");
					$("#details_loginType").attr("src","../../resources/images/icons/stop.png");
				} else if(data.rows[0].loginResult=='3'){
					$("#details_loginResult").text("登录失败：该用户账户密码错误!");
					$("#details_loginType").attr("src","../../resources/images/icons/delete.png");
				}else{
					$("#details_loginType").attr("src","");
				}
				$("#details_loginTime").text(data.rows[0].loginTime==null ? "" : data.rows[0].loginTime);
				$("#details_onlineTime").text(data.rows[0].onlineTime==null ? "" : data.rows[0].onlineTime);
				$("#details_browserMessage").text(data.rows[0].browserMessage==null ? "" : data.rows[0].browserMessage);
				$("#details_createTime").text(data.rows[0].createTime==null ? "" : data.rows[0].createTime);
			}
		});
		$("#loginlogFrame").dialog('open');
	} else {
		noSelectData();
	}
}
